package com.exsun.ex1077.ui.activity.login

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.EncryptUtils
import com.exsun.ex1077.app.Constant
import com.exsun.ex1077.network.apiService
import com.exsun.ex1077.network.entity.Login
import com.zhangping.fastjetpack.base.BaseViewModel
import com.zhangping.fastjetpack.extend.base.ResultState
import com.zhangping.fastjetpack.extend.base.request
import com.zhangping.fastjetpack.extend.databind.BooleanObservableField
import com.zhangping.fastjetpack.extend.databind.StringObservableField
import com.zhangping.fastjetpack.extend.logI

/**
 * 描述：
 * 作者：ZhangPing
 * 创建时间：2022/8/5 16:58
 */
class LoginViewModel: BaseViewModel() {

    val pwdShow = BooleanObservableField()
    val remember = BooleanObservableField()
    val agreement = BooleanObservableField()

    val account = StringObservableField()
    val password = StringObservableField()

    val loginResult = MutableLiveData<ResultState<Login>>()

    fun login(){
        request({apiService.login(account.get(), getAesPassword(password.get()))}, loginResult, true)
    }

    private fun getAesPassword(password: String): String{
        return EncryptUtils.encryptAES2HexString(password.toByteArray(),
            Constant.PASSWORD_AES_SECRET.toByteArray(), "AES/ECB/PKCS5Padding", null)
    }

}